﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace ClientApp
{
    public partial class EmployerRegForm : Form
    {
        public EmployerRegForm()
        {
            InitializeComponent();
        }

        private void EmployerRegistrationDoneButton_Click(object sender, EventArgs e)
        {
            if (TitleMaskedTextBox.Text == "" ||
                ActivitiesCheckedListBox.SelectedItems.Count == 0 ||
                AddressMaskedTextBox.Text == "" ||
                PhoneMaskedTextBox.Text == "" ||
                LoginTextBox.Text == "")
            {
                MessageBox.Show("Заполните все поля формы.", "Ошибка");
                return;
            }

            if (DatabaseAccess.Instance.EmployerExists(LoginTextBox.Text))
            {
                MessageBox.Show("Введенный логин уже существует в системе", "Ошибка");
                return;
            }

            List<Activity> checked_activities = (from object item in ActivitiesCheckedListBox.CheckedItems 
                                                 select DatabaseAccess.Instance.ActivityByTitle(item as string)).ToList();

            DatabaseAccess.Instance.Add(new Employer(TitleMaskedTextBox.Text,
                                                     checked_activities,
                                                     AddressMaskedTextBox.Text,
                                                     PhoneMaskedTextBox.Text,
                                                     LoginTextBox.Text));

            MessageBox.Show("Работодатель успешно зарегистрирован.");
            Close();
        }
    }
}
